package com.ming.transaction.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ming.transaction.common.Result;
import com.ming.transaction.entity.Classify;
import com.ming.transaction.entity.Role;
import com.ming.transaction.mapper.ClassifyMapper;
import com.ming.transaction.service.IClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static com.ming.transaction.common.Constant.ROLE_CLASSIFY;
import static com.ming.transaction.common.Constant.ROLE_LIST;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author 陈仕健
 * @since 2023-05-11
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements IClassifyService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询所有分类
     * @return
     */
    @Override
    public Result findAll() {
        String cla = stringRedisTemplate.opsForValue().get(ROLE_CLASSIFY);
        if (StrUtil.isNotBlank(cla)){
            List<Classify> clas = JSONUtil.toList(cla,Classify.class);
            return Result.success("查询分类成功！",clas);
        }
        List<Classify> classifies = list();
        if (classifies == null){
            return Result.fail("分类为空！");
        }
        stringRedisTemplate.opsForValue().set(ROLE_CLASSIFY,JSONUtil.toJsonStr(classifies));
        return Result.success("查询分类成功！",classifies);
    }

    /**
     * 添加分类
     * @param classify
     * @return
     */
    @Override
    public Result addClasisfy(Classify classify) {
        boolean save = save(classify);

        if (!save) {
            return Result.fail("添加分类失败!");
        }
        // 缓存中获取数据
        String sr = stringRedisTemplate.opsForValue().get(ROLE_CLASSIFY);
        List<Classify> classifies = JSONUtil.toList(sr, Classify.class);
        classifies.add(classify);
        // 添加分类成功 将数据保存到 Redis
        stringRedisTemplate.opsForValue().set(ROLE_CLASSIFY, JSONUtil.toJsonStr(classifies));

        return Result.success("添加分类成功！", classifies);
    }

    /**
     * 批量删除分类
     * @param ids
     * @return
     */
    @Override
    public Result delClassifys(List<Integer> ids) {
        if (!removeByIds(ids)){
            return Result.fail("批量删除分类失败!");
        }
        List<Classify> list = list();
        stringRedisTemplate.opsForValue().set(ROLE_CLASSIFY,JSONUtil.toJsonStr(list));
        return  Result.success("批量删除分类成功!");
    }


    /**
     * 单删分类
     * @param id
     * @return
     */
    @Override
    public Result delClassify(Integer id) {
        if (!removeById(id)){
            return Result.fail("分类删除失败!");
        }
        List<Classify> list = list();
        stringRedisTemplate.opsForValue().set(ROLE_CLASSIFY,JSONUtil.toJsonStr(list));
        return  Result.success("分类删除成功!");
    }

    /**
     * 修改分类信息
     * @param classify
     * @return
     */
    @Override
    public Result updateClassify(Classify classify) {
        if (!saveOrUpdate(classify)) {
            return Result.fail("修改分类信息失败!");
        }
        // 修改成功后将 Redis 数据进行修改
        // 查询数据库直接修改 redis
        List<Classify> list = list();
        stringRedisTemplate.opsForValue().set(ROLE_CLASSIFY,JSONUtil.toJsonStr(list));
        return Result.success("修改分类信息成功!",classify);
    }
}
